package com.cytech.cloudclinic.moudle.main.drug;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.DrugAdapter;
import com.cytech.cloudclinic.adapter.DrugTagAdapter;
import com.cytech.cloudclinic.base.BaseFragment;
import com.cytech.cloudclinic.bean.Drug;
import com.cytech.cloudclinic.bean.DrugTag;
import com.cytech.cloudclinic.bean.event.RefreshDrug;
import com.cytech.cloudclinic.bean.event.UpdateAddTagEvent;
import com.cytech.cloudclinic.moudle.main.drug.add.AddDrugActivity;
import com.cytech.cloudclinic.moudle.main.drug.add2.AddDrugActivity2;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_6;

public class DrugManagerFragment extends BaseFragment<DrugManagerPresenter> implements DrugManagerView {
    @BindView(R.id.rg_drug_manager)
    RadioGroup radioGroup;

    @BindView(R.id.rv_drug_child_tag)
    RecyclerView rvDrugChildTag;
    @BindView(R.id.ll_add_drug)
    LinearLayout llAddPatient;
    @BindView(R.id.rv_drug_list)
    RecyclerView rvDrugList;

    private List<DrugTag> drugTagList;
    private List<Drug> drugList;
    private DrugTagAdapter drugTagAdapter;
    private DrugAdapter drugAdapter;


    private int childId = 10;
    private int parentId = 10;


    private int currentFragmentIndex = 0;
    private List<Fragment> fragmentList;
    private Fragment currentFragment;

    public static DrugManagerFragment newInstance() {
        DrugManagerFragment fragment = new DrugManagerFragment();
        //如果有参数，使用Bundle传递
        return fragment;
    }


    @Override
    protected DrugManagerPresenter createPresenter() {
        return new DrugManagerPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_drug;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        drugTagList = new ArrayList<>();
        drugTagAdapter = new DrugTagAdapter(drugTagList);
        drugTagAdapter.setCurrentPos(0);
        FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(getActivity());
        flexboxLayoutManager.setFlexDirection(FlexDirection.ROW);//主轴为水平方向，起点在左端。
        flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);//按正常方向换行
        rvDrugChildTag.setLayoutManager(flexboxLayoutManager);
        rvDrugChildTag.setAdapter(drugTagAdapter);
        drugTagAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                childId = drugTagAdapter.getItem(position).tag_id;
                drugTagAdapter.setCurrentPos(position);
                presenter.getDrugList("",drugTagAdapter.getItem(position).tag_id);
            }
        });

        drugList = new ArrayList<>();
        drugAdapter = new DrugAdapter(drugList,parentId);
        rvDrugList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvDrugList.setAdapter(drugAdapter);
        drugAdapter.addChildClickViewIds(R.id.item_drug_detail);
        drugAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if(parentId == DRUG_TAG_6){
                    AddDrugActivity.actionStart(getActivity(),childId,drugAdapter.getItem(position).drug_id,parentId);
                }else{
                    AddDrugActivity2.actionStart(getActivity(),childId,drugAdapter.getItem(position).drug_id,parentId);
                }

            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb_drug_1:
                        parentId = 10;
                        break;
                    case R.id.rb_drug_2:
                        parentId = 4;
                        break;
                    case R.id.rb_drug_3:
                        parentId = 2;
                        break;
                    case R.id.rb_drug_4:
                        parentId = 3;
                        break;
                    case R.id.rb_drug_5:
                        parentId = 5;
                        break;
                    case R.id.rb_drug_6:
                        parentId = 1;
                        break;
                }
                presenter.getDrugTagList(parentId);
            }
        });
    }

    @Override
    protected void initData() {
        presenter.getDrugTagList(parentId);
    }

    @Override
    public void getDrugChildTagSuccess(List<DrugTag> data) {
        if(data!=null&&data.size()>0){
            childId = data.get(0).tag_id;
        }
        drugTagAdapter.setNewInstance(data);
        if(data!=null&&data.size()>0){
            presenter.getDrugList("",data.get(0).tag_id);
        }
    }

    @Override
    public void getDrugSuccess(List<Drug> data) {
        drugAdapter.setNewInstance(data);
    }

    @OnClick(R.id.ll_add_drug)
    public void addDrug(View view){
        if(parentId == DRUG_TAG_6){
            //如果是中西成药
            AddDrugActivity.actionStart(getActivity(),childId,0,parentId);
        }else{
            AddDrugActivity2.actionStart(getActivity(),childId,0,parentId);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateData(RefreshDrug event) {
        presenter.getDrugList("",childId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
